package ejb.manager;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import ejb.dao.PositionDAO;
import ejb.entity.DefaultPosition;
import ejb.entity.Position;
import ejb.entity.User;

@Stateless
public class DefaultPositionManager implements PositionManager {
	@EJB
	private PositionDAO pdao;

	@Override
	public Position addPosition(String position_name, User user, int x, int y){
		Position newPosition = pdao.findByNameAndUser(position_name, user);
		if(newPosition == null){	
			System.out.println("Wpis: " + position_name + user.getUserName() + x + " - "+ y);
			newPosition = new DefaultPosition(position_name, x, y, user);
			newPosition = pdao.create(newPosition);			
			return newPosition;
		}else return null;			
	}
	
	@Override
	public Position getPositionById(int id){
		Position position = pdao.findById(id);
		
		if(position == null) return null;
		else return position;			
	}	
	
	@Override
	public List<Position> getUserPositions(User user){
		List<Position> userPositions = pdao.findByUser(user);
		
		if(userPositions == null) return null;
		else return userPositions;			
	}
	
	@Override
	public void setForecast(int id, boolean state){
		Position position = pdao.findById(id);
		position.setForecast(state);
	}
	
	@Override
	public boolean deletePosition(int position_id){
		Position position = pdao.findById(position_id);
		boolean state = pdao.delete(position);
		return state;				
	}
}
